package energyware.queries;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import energyware.sqlite.SQLite;
import energyware.utils.ProtocolSeparator;

public class Queries {

	
	
	public void insertLogin(String email, String password, String type, Context context){
		
	   SQLite usdbh = new SQLite(context , "Energyware", null, 1);
	   SQLiteDatabase db = usdbh.getWritableDatabase();
	   if(db != null)
	   {
		   db.execSQL("INSERT INTO User (password, email, type) " +
 				 "VALUES ('" + password + "' , '" +
 				 email + "' , '" + type +"')");		            				
	   }
	   
	   db.close();
	   
	}
	
	public void insertComputersWithGPS(String computersWithGPS, Context context){
		
	   SQLite usdbh = new SQLite(context , "Energyware", null, 1);
	   SQLiteDatabase db = usdbh.getWritableDatabase();
	   if(db != null)
	   {
		   for(int i = 0; i < computersWithGPS.split(ProtocolSeparator.ROWS).length; i++){
		   db.execSQL("INSERT INTO ComputersWithGPS (id, latitude, longitude) " +
 				 "VALUES ('" + computersWithGPS.split(ProtocolSeparator.ROWS)[i].split(ProtocolSeparator.COLUMNS)[0] + "' , '" +
 				computersWithGPS.split(ProtocolSeparator.ROWS)[i].split(ProtocolSeparator.COLUMNS)[1] + "' , '" + computersWithGPS.split(ProtocolSeparator.ROWS)[i].split(ProtocolSeparator.COLUMNS)[2] +"')");
		   }	            				
	   }
	   
	   db.close();
	   
	}
	
	public void insertDefaultConfig(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getWritableDatabase();
	    if(db != null)
		   {
	    		db.execSQL("INSERT INTO Config (paramName, value) " +
	 				 "VALUES ('GPSTIME' , '2MIN')");		                				
		   }
		   
		 db.close();
		
	}
	
	public void updateGPSTIMEConfig(Context context, String time){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getWritableDatabase();
	    if(db != null)
		   {
	    		db.execSQL("UPDATE Config SET value = '" +  time + "MIN' WHERE paramName = 'GPSTIME'");		                				
		   }
		   
		 db.close();
		
	}
	
	public void updateComputerGPS(Context context, String id, String latitude, String longitude){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getWritableDatabase();
	    if(db != null)
		   {
	    		db.execSQL("UPDATE ComputersWithGPS SET latitude = '" +  latitude + "' , longitude = '" + longitude + "' " +
	    				   " WHERE id = '"+ id + "'");		                				
		   }
		   
		 db.close();
		
	}
	
	public void addGPSComputer(Context context, String id, String latitude, String longitude){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getWritableDatabase();
	    if(db != null)
		   {
	    	db.execSQL("INSERT INTO ComputersWithGPS (id, latitude, longitude) VALUES ('"+
					id + "','"+ latitude+"','"+longitude+"')");              				
		   }
		   
		 db.close();
		
	}
	
	public boolean checkGPSComputer(Context context, String id){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT COUNT(*) as cantidad FROM ComputersWithGPS WHERE id = '" + id + "'" , null);
	    boolean isGPS = false;
	    if (cursor.moveToFirst()) {
	    	String cantidad = cursor.getString(cursor.getColumnIndex("cantidad"));
	    	
	    	if(Integer.valueOf(cantidad)>0){
	    		isGPS = true;
	    	}
	    }
	    db.close();
	    cursor.close();
	    
	    return isGPS;
		
	}
	
	public String getGPSTIMEConfig(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT * FROM Config WHERE paramName = 'GPSTIME'", null);
	    String time = "";
	    if (cursor.moveToFirst()) {
	    	time =  cursor.getString(cursor.getColumnIndex("value"));
	    }
	    db.close();
	    cursor.close();
	    
	    return time;
	}
		
	public boolean checkConfig(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT COUNT(*) as cantidad FROM Config", null);
	    boolean isConfig = false;
	    if (cursor.moveToFirst()) {
	    	String cantidad = cursor.getString(cursor.getColumnIndex("cantidad"));
	    	
	    	if(Integer.valueOf(cantidad)>0){
	    		isConfig = true;
	    	}
	    }
	    db.close();
	    cursor.close();
	    
	    return isConfig;
		
	}
		
	public void logOut(Context context){
		
	   SQLite usdbh = new SQLite(context , "Energyware", null, 1);
	   SQLiteDatabase db = usdbh.getWritableDatabase();
	   if(db != null)
	   {
		   db.execSQL("DELETE FROM User");	
		   db.execSQL("DELETE FROM ComputersWithGPS");	
	   }
	   
	   db.close();
	}
		
	public boolean isLogged(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT COUNT(*) as cantidad FROM User", null);
	    boolean isLogged = false;
	    if (cursor.moveToFirst()) {
	    	String cantidad = cursor.getString(cursor.getColumnIndex("cantidad"));
	    	
	    	if(Integer.valueOf(cantidad)>0){
	    		isLogged = true;
	    	}
	    }
	    db.close();
	    cursor.close();
	    
	    return isLogged;
		
	}
		
	public String getEmail(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT * FROM User", null);
	    String email = "";
	    if (cursor.moveToFirst()) {
	    	email =  cursor.getString(cursor.getColumnIndex("email"));
	    }
	    db.close();
	    cursor.close();
	    
	    return email;
	}
		
	public String getType(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT * FROM User", null);
	    String type = "";
	    if (cursor.moveToFirst()) {
	    	type =  cursor.getString(cursor.getColumnIndex("type"));
	    }
	    db.close();
	    cursor.close();
	    
	    return type;
	}
	
	public String getComputersGPS(Context context){
		SQLite usdbh = new SQLite(context, "Energyware", null, 1);
		SQLiteDatabase db = usdbh.getReadableDatabase();
	    Cursor cursor = db.rawQuery("SELECT * FROM ComputersWithGPS", null);
	    String computersGPS = "";
	    cursor.moveToFirst();
	    while (cursor.isAfterLast() == false){
		  
		    	computersGPS =  computersGPS + cursor.getString(cursor.getColumnIndex("id")) + 
		    			ProtocolSeparator.COLUMNS + cursor.getString(cursor.getColumnIndex("latitude")) + 
		    			ProtocolSeparator.COLUMNS + cursor.getString(cursor.getColumnIndex("longitude")) + 
		    			ProtocolSeparator.ROWS;
		    	cursor.moveToNext();
	    }
	    db.close();
	    cursor.close();
	    
	    return computersGPS;
	}
	
}
